package com.nannybear.Model;

import com.nannybear.MainActivity;

import java.util.ArrayList;
import java.util.Observer;

/**
 * @brief Own the modifiable thresholds of light. Observable by ProxyRobot.
 */
public class LightThreshold extends Constant{
    /********** DECLARATIONS **********/
    private MainActivity mActivity;
    private String lightThresholds = null;
    private double darkDimThreshold;
    private double dimBrightThreshold;
    private ArrayList<Observer> observers;


    /************ METHODS ************/
    /**
     * @brief constructor
     */
    public LightThreshold(MainActivity activity){
        this.mActivity = activity;

        darkDimThreshold = 50d;
        dimBrightThreshold = 75d;

        this.observers = new ArrayList<>();
    }

    /**
     * @brief add the class as observable
     */
    public void addObserver(Observer obs){
        observers.add(obs);
    }

    /**
     * @brief delete the class as observable
     */
    public synchronized void deleteObserver(Observer obs){
        observers.remove(obs);
    }

    /**
     * @brief notify the observers
     */
    public void notifyObservers(){
        for (int i = 0; i < observers.size(); i++) {
            observers.get(i).update(this, getLightThresholds());
        }
    }

    /**
     * @brief save the new thresholds of lights
     */
    public void saveNewThresholds(String lightThresholds){
        //TODO enregistre les nouveaux threshold et fait un setDimBrightThreshold + setDarkDimThreshold
        String storage[] = lightThresholds.toString().split("/");
        setDarkDimThreshold(Double.parseDouble(storage[0]));
        setDimBrightThreshold(Double.parseDouble(storage[1]));
    }

    /*********** ACCESSORS ***********/
    public String getLightThresholds() {
        return lightThresholds;
    }

    public void setLightThresholds(String lightThresholds) {
        this.lightThresholds = lightThresholds;
        notifyObservers();
        saveNewThresholds(lightThresholds);
    }

    public double getDarkDimThreshold() {
        return darkDimThreshold;
    }

    public void setDarkDimThreshold(double darkDimThreshold) {
        this.darkDimThreshold = darkDimThreshold;
    }

    public double getDimBrightThreshold() {
        return dimBrightThreshold;
    }

    public void setDimBrightThreshold(double dimBrightThreshold) {
        this.dimBrightThreshold = dimBrightThreshold;
    }
}
